import Vue from 'vue'
import Router from 'vue-router'
import LoginLayout from '@/views/login/layout/LoginLayout'

Vue.use(Router)
import Layout from '@/layout'
import WebLayout from '@/layout/WebLayout'

// 静态通用路由
export const constantRoutes = [
  {
    path: '/login',
    component: LoginLayout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'Login',
        component: () => import('@/views/login/index'),
        hidden: true,
        meta: {
          title: '用户登录'
        }
      }
    ]
  },

  {
    path: '/sync/:token/:roleType',
    name: 'LoginSync',
    component: () => import('@/views/login/sync'),
    hidden: true,
    meta: { title: '登录成功', noCache: true }
  },

  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人资料', icon: 'user', noCache: true }
      }
    ]
  },

  {
    path: '/',
    component: WebLayout,
    redirect: '/web/index',
    hidden: true,
    children: [
      {
        path: '/web/index',
        component: () => import('@/views/web/index/list'),
        name: 'indexList',
        meta: { title: '首页', icon: 'user', noCache: true }
      },
      {
        path: '/web/index/view/:docId',
        component: () => import('@/views/web/index/view'),
        name: 'docView',
        meta: { title: '文档详情', noCache: true, activeMenu: '/web/index' }
      },
      {
        path: '/web/newslist',
        component: () => import('@/views/web/news/list'),
        name: 'newsList',
        meta: { title: '新闻消息', icon: 'user', noCache: true }
      },
      {
        path: '/web/news/view/:newsId',
        component: () => import('@/views/web/news/view'),
        name: 'newsView',
        meta: { title: '新闻详情', noCache: true, activeMenu: '/news/list' }
      },

      {
        path: '/web/uc',
        component: () => import('@/views/web/ucenter'),
        name: 'UserCenter',
        meta: { title: '用户中心', noCache: true }
      },

      {
        path: '/web/notice',
        component: () => import('@/views/web/notice'),
        name: 'NoticeList',
        meta: { title: '系统公告', noCache: true }
      },
      {
        path: '/web/notice/detail/:id',
        component: () => import('@/views/web/notice/detail'),
        name: 'NoticeDetail',
        meta: { title: '公告详情', noCache: true, activeMenu: '/web/notice' }
      },
      {
        path: '/web/doc',
        component: () => import('@/views/web/doc/list'),
        name: 'myDoc',
        meta: { title: '我的资料', noCache: true, activeMenu: '/web/doc' }
      },

      {
        path: '/web/doc/addDoc',
        component: () => import('@/views/web/doc/addDoc'),
        name: 'addDoc',
        meta: { title: '添加资料', noCache: true, activeMenu: '/web/doc' }
      },
      {
        path: '/web/doc/addDoc/:id',
        component: () => import('@/views/web/doc/addDoc'),
        name: 'updateDoc',
        meta: { title: '修改资料', noCache: true, activeMenu: '/web/doc' }
      }
    ]
  }
  ]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
